package main

import (
	"fmt"
	"math"
	"sort"
)

type PlaneFigure interface {
	area() float64
}

type PlaneFigureSort struct {
	Name string
	Area float64
}

type Circle struct {
	Diameter float64
}

type Triangle struct {
	Bottom float64
	High   float64
}

type Rectangle struct {
	Long  float64
	Width float64
}

func (c *Circle) area() float64 {
	return c.Diameter * math.Pi
}

func (t *Triangle) area() float64 {
	return t.Bottom * t.High / 2
}

func (r *Rectangle) area() float64 {
	return r.Long * r.Width
}

func Area(f PlaneFigure) float64 {
	return f.area()
}

func main() {
	m := make(map[string]float64, 3)
	m["circle"] = Area(&Circle{Diameter: 50})
	m["triangle"] = Area(&Triangle{Bottom: 10, High: 10})
	m["rectangle"] = Area(&Rectangle{Long: 10, Width: 10})
	// 1.实现对圆形,长方形,三角形求面积
	fmt.Println("排序前: ", m)

	orderly := make([]PlaneFigureSort, 0, 3)
	for k, v := range m {
		orderly = append(orderly, PlaneFigureSort{Name: k, Area: v})
	}
	sort.Slice(orderly, func(i, j int) bool {
		return orderly[i].Area > orderly[j].Area
	})
	// 2.利用第1题，构造3个以上图形，至少圆形、三角形、矩形各有一个，对上题的图形按照面积降序排列
	fmt.Println("排序后: ", orderly)
}

// 阿飞老师的批改
// 1. 圆的面积计算方式是PI*r^2，不是2PI*r，diameter是直径；
